<?php
/**
 * Interface to add/remove courses from the system
 */
?>
<html>
<head>

</head>
<body>
<?php
require_once '../dbms_university_courses_mvc/Model/model_require_once.php';
?>
<h2>University Courses</h2>
<?php
$courses = Course::getCourses ();

//Setup the Course Javascript objects, these objects are required for updates and delections of the underlying database rows 
Course::instantiate_javascript_objects($courses);
?>
<table>
	<tr>
		<th>Name</th>
		<th>Instructor</th>
		<th>Department</th>
		<th>Term</th>
		<th>Credits</th>
		<th>Classroom</th>
		<th>Edit</th>
		<th>Delete</th>
	</tr>
<?php
//Iterate through the terms and print an input setter and delete button for each
//Refresh the page content on deletion
//refreshPage/reloadPage functions can be found in the js_include/index_functions.js page
//refreshPage could easily reload a much smaller portion of the page
//these rows are not editable, except to be deleted. They could be made editable by getting the setters.
/* @var $course Course */ 
foreach ( $courses as $course ) {
	?>
	<tr>
		<td><?php
	echo $course->get_course_name ();
	?></td>
		<td><?php
	//instead of showing instructor_ids, which are stored in the database row, get the actual instructor object
	//and show the first and last name
	/* @var $instructor Instructor */
	$instructor_id = $course->get_instructor_id ();
	if (isset ( $instructor_id )) {
		$instructor = Instructor::constructFrom_instructor_id ( $instructor_id );
		$profile = Profile::constructFrom_profile_id ( $instructor->get_profile_id () );
		echo $profile->get_first_name () . " " . $profile->get_last_name ();
	}
	?></td>
		<td><?php
	//instead of showing department_ids, which are stored in the database row, get the actual department object
	//and show the department name		
	/* @var $department Department */
	$department_id = $course->get_department_id ();
	if (isset ( $department_id )) {
		$department = Department::constructFrom_department_id ( $department_id );
		echo $department->get_department_name ();
	}
	?></td>
		<td><?php
	//get the term name and year (eg fall 2010)
	/* @var $term Term */
	$term_id = $course->get_term_id ();
	if (isset ( $term_id )) {
		$term = Term::constructFrom_term_id ( $term_id );
		echo $term->get_term () . " " . $term->get_year ();
	}
	?></td>
		<td><?php
	echo $course->get_credits ();
	?></td>
		<td><?php
	echo $course->get_classroom_id ();
	?></td>
	<td>
	<a href="index.php#admin_edit_course.php?course_id=<?php echo $course->get_course_id(); ?>" target="_blank">Edit</a>
	</td>
		<td>
		<?php $course->print_delete_button(true, "refreshPage");?>
		</td>
	</tr>
	<!-- The prerequisites for this course -->
	<tr>
		<td></td>
		<td colspan="6">
		<h2>Prerequisites</h2>
		</td>
	</tr>
	<?php
	//get all the prereq objects
	/* @var $coursePrereq CoursePrerequisite */
	$coursePrereqs = CoursePrerequisite::getCoursePrerequisites_by_course_id ( $course->get_course_id () );
	//iterate through the course prereqs, and print them to display
	//also make it so that they can be removed from the list of prereqs
	foreach ( $coursePrereqs as $coursePrereq ) {
		//construct the prereq course from the preq course's id
		/* @var $prereqCourse Course */
		$prereqCourse = Course::constructFrom_course_id ( $coursePrereq->get_prerequisite_course_id() );
		?>
		<tr>
		<td></td>
		<td></td>
		<td colspan="2"><em><?php
		echo $prereqCourse->get_course_name ();
		?></em></td>
		<td>
		<?php //print out the javascript object for this single course prereq
		$coursePrereq->instantiate_javascript_object(); 
		$coursePrereq->print_delete_button(true, "refreshPage");
		?>
		</td>
	</tr>
	<?php
	}
	?>
	<tr>
		<td>
<!-- Create the javascript objects for the prereq courses -->
<script type="text/javascript">
		var course<?php
	echo $course->get_course_id ();
	?>_prereq_id;
	function setCourse<?php echo $course->get_course_id() ?>PrereqId(id){
		course<?php
				echo $course->get_course_id ();
				?>_prereq_id = id;
	}
	</script></td>
		<td colspan="6"><?php
		//print out a course dropdown so one can select a course to add as a prereq
		Course::print_Course_dropdown(Course::getCourses(), "course_name", "", "setCourse".$course->get_course_id()."PrereqId");
	?>
<!-- Adds a new course as a prerequisite for the course with the id of $course->get_course_id -->
	<input align="left" class="ui-corner-all ui-state-default"
	type="button"
	onclick="insertNewCoursePrerequisite(<?php echo $course->get_course_id(); ?>, course<?php echo $course->get_course_id (); ?>_prereq_id, refreshPage);"
	value="Add Prereq"
/>
<br><br>
	</td>
	</tr>
	<?php
}

//insert new course via ajax call
?>
</table>
<br>
<br>


<h2>Add Course</h2>
<script type="text/javascript">
//the setter functions for saving values before a new course is inserted
//this is a good example of using dropdowns in an insertion
var course_name;
function setCourseName(name){
	course_name = name;
}

var instructor_id;

var departmentId;
function setDepartmentId(id){
	department_id = id;
}

var term_id;
function setTermId(id){
	term_id = id;
}

var credits = 3;
function setCredits(num){
	credits = num;
}

var classroom_id;
function setClassroomId(id){
	classroom_id = id;
}

</script>
<!-- Use 'Course Name' as the default value, make that value disappear when someone puts their cursor in the input area -->
<input onfocus="if(this.value == 'Course Name'){
	this.value = '';
	}"
	onblur="setCourseName(this.value);" size=12 value='Course Name'
/>

<?php
//print a dropdown with all the available list of departments, 
//on a change of the dropdown, save the department_id using the javascript setDepartmentId function
Department::print_Department_dropdown ( Department::getDepartments (), "department_name", "", "setDepartmentId" );
?>

<!--Print a dropdown of the terms, had to manually print the dropdown because the display needs to be multiple fields-->
<select onchange="setTermId(this.value)">
	<option>Select Term</option>
<?php
$terms = Term::getTerms ();
foreach ( $terms as $term ) {
	?>
<option value="<?php
	echo $term->get_term_id ();
	?>"><?php
	echo $term->get_term () . " " . $term->get_year ();
	?></option>
<?php
}
?>
</select>
<input
	onblur="
if(isNaN(this.value)){
	this.value = credits;
	alert('Number of credits must be a number');
}
else{
	setCredits(this.value);
}"
	size=2 value='3'
/>

<?php
//print the list of classrooms
Classroom::print_Classroom_dropdown ( Classroom::getClassrooms (), "classroom_id", "", "setClassroomId" );
?>
<!-- Use the javascript function which calls the webservice to insert a new course, pass all the saved variables. May want to do additional validation, for example, this doesn't check if you chose a classroom before submitting. -->
<input align="right" class="ui-corner-all ui-state-default"
	type="button"
	onclick="insertNewCourse(instructor_id, department_id, term_id, credits, classroom_id, course_name, refreshPage);"
	value="Add Course"
/>

</body>
</html>